# Script by D walker, bygonebytes.co.uk 2026
import board
import keypad
import digitalio
 
# Define outputs to Maplin 5600
ledA = digitalio.DigitalInOut(board.GP21)
ledA.direction = digitalio.Direction.OUTPUT
ledB = digitalio.DigitalInOut(board.GP20)
ledB.direction = digitalio.Direction.OUTPUT
ledC = digitalio.DigitalInOut(board.GP19)
ledC.direction = digitalio.Direction.OUTPUT
ledD = digitalio.DigitalInOut(board.GP18)
ledD.direction = digitalio.Direction.OUTPUT
ledE = digitalio.DigitalInOut(board.GP17)
ledE.direction = digitalio.Direction.OUTPUT
ledF = digitalio.DigitalInOut(board.GP16)
ledF.direction = digitalio.Direction.OUTPUT

ledKDn = digitalio.DigitalInOut(board.GP22)
ledKDn.direction = digitalio.Direction.OUTPUT

# Footpeddle input - Pull down not required when circuit Board is corrected.
footswitch = digitalio.DigitalInOut(board.GP27)
footswitch.direction = digitalio.Direction.INPUT
footswitch.pull = digitalio.Pull.UP

# Input to switch keyboard up an octave.
octave = digitalio.DigitalInOut(board.GP28)
octave.direction = digitalio.Direction.INPUT
octave.pull = digitalio.Pull.UP


# Key Count for keydown to ensure KEYDOWN is logic 0 only after the last key is released.
KC = 0

# Define pins for rows and columns
rows = (board.GP7, board.GP8, board.GP9, board.GP10,board.GP11, board.GP12,board.GP13, board.GP14, board.GP15)
cols = (board.GP6, board.GP5, board.GP4, board.GP3,board.GP2, board.GP1)

# Initialize KeyMatrix
keys = keypad.KeyMatrix(row_pins=rows, column_pins=cols)

def KeyCount():
    global KC
    KC = KC - 1
    if KC > 0 or footswitch.value:
        ledKDn.value = True
    else: 
        ledKDn.value = False

while True:
    event = keys.events.get()
    if event:
        if event.pressed:
            if event.key_number == 5:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledA.value = False
                else:
                    ledA.value = True
                    ledC.value = True
                    ledD.value = True
                    ledA.value = False
                    ledC.value = False
                    ledD.value = False
            elif event.key_number == 6:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledB.value = False
                else:
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
            elif event.key_number == 7:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledA.value = False
                    ledB.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
            elif event.key_number == 8:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledC.value = True
                    ledC.value = False
                else:
                    ledE.value = True
                    ledE.value = False
            elif event.key_number == 9:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledC.value = True
                    ledA.value = False
                    ledC.value = False
                else:
                    ledA.value = True
                    ledE.value = True
                    ledA.value = False
                    ledE.value = False
            elif event.key_number == 10:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledC.value = True
                    ledB.value = False
                    ledC.value = False
                else:
                    ledB.value = True
                    ledE.value = True
                    ledB.value = False
                    ledE.value = False
            elif event.key_number == 11:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledE.value = False
            elif event.key_number == 12:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledD.value = True
                    ledD.value = False
                else:
                    ledC.value = True
                    ledE.value = True
                    ledC.value = False
                    ledE.value = False
            elif event.key_number == 13:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledD.value = True
                    ledA.value = False
                    ledD.value = False
                else:
                    ledA.value = True
                    ledC.value = True
                    ledE.value = True
                    ledA.value = False
                    ledC.value = False
                    ledE.value = False
            elif event.key_number == 14:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledD.value = True
                    ledB.value = False
                    ledD.value = False
                else:
                    ledB.value = True
                    ledC.value = True
                    ledE.value = True
                    ledB.value = False
                    ledC.value = False
                    ledE.value = False
            elif event.key_number == 15:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledD.value = True
                    ledA.value = False
                    ledB.value = False
                    ledD.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledE.value = False
            elif event.key_number == 16:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledC.value = True
                    ledD.value = True
                    ledC.value = False
                    ledD.value = False
                else:
                    ledD.value = True
                    ledE.value = True
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 17:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledC.value = True
                    ledD.value = True
                    ledA.value = False
                    ledC.value = False
                    ledD.value = False
                else:
                    ledA.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 18:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                else:
                    ledB.value = True
                    ledD.value = True
                    ledE.value = True
                    ledB.value = False
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 19:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 20:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledE.value = True
                    ledE.value = False
                else:
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 21:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledE.value = True
                    ledA.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 22:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledE.value = True
                    ledB.value = False
                    ledE.value = False
                else:
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 23:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
            elif event.key_number == 24:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledC.value = True
                    ledE.value = True
                    ledC.value = False
                    ledE.value = False
                else:
                    ledF.value = True
                    ledF.value = False
            elif event.key_number == 25:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledC.value = True
                    ledE.value = True
                    ledA.value = False
                    ledC.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledF.value = True
                    ledA.value = False
                    ledF.value = False
            elif event.key_number == 26:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledC.value = True
                    ledE.value = True
                    ledB.value = False
                    ledC.value = False
                    ledE.value = False
                else:
                    ledB.value = True
                    ledF.value = True
                    ledB.value = False
                    ledF.value = False
            elif event.key_number == 27:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledF.value = False
            elif event.key_number == 28:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledD.value = True
                    ledE.value = True
                    ledD.value = False
                    ledE.value = False
                else:
                    ledC.value = True
                    ledF.value = True
                    ledC.value = False
                    ledF.value = False
            elif event.key_number == 29:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledD.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledC.value = True
                    ledF.value = True
                    ledA.value = False
                    ledC.value = False
                    ledF.value = False
            elif event.key_number == 30:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledD.value = True
                    ledE.value = True
                    ledB.value = False
                    ledD.value = False
                    ledE.value = False
                else:
                    ledB.value = True
                    ledC.value = True
                    ledF.value = True
                    ledB.value = False
                    ledC.value = False
                    ledF.value = False
            elif event.key_number == 31:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledD.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledF.value = False
            elif event.key_number == 32:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
                else:
                    ledD.value = True
                    ledF.value = True
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 33:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 34:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
                else:
                    ledB.value = True
                    ledD.value = True
                    ledF.value = True
                    ledB.value = False
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 35:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 36:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledF.value = True
                    ledF.value = False
                else:
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 37:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledF.value = True
                    ledA.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 38:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledF.value = True
                    ledB.value = False
                    ledF.value = False
                else:
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 39:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
            elif event.key_number == 40:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledC.value = True
                    ledF.value = True
                    ledC.value = False
                    ledF.value = False
                else:
                    ledE.value = True
                    ledF.value = True
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 41:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledC.value = True
                    ledF.value = True
                    ledA.value = False
                    ledC.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 42:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledC.value = True
                    ledF.value = True
                    ledB.value = False
                    ledC.value = False
                    ledF.value = False
                else:
                    ledB.value = True
                    ledE.value = True
                    ledF.value = True
                    ledB.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 43:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 44:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledD.value = True
                    ledF.value = True
                    ledD.value = False
                    ledF.value = False
                else:
                    ledC.value = True
                    ledE.value = True
                    ledF.value = True
                    ledC.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 45:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledD.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledC.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledC.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 46:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledD.value = True
                    ledF.value = True
                    ledB.value = False
                    ledD.value = False
                    ledF.value = False
                else:
                    ledB.value = True
                    ledC.value = True
                    ledE.value = True
                    ledF.value = True
                    ledB.value = False
                    ledC.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 47:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledD.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 48:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
                else:
                    ledD.value = True
                    ledE.value = True
                    ledF.value = True
                    ledD.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 49:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledD.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledD.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 50:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
                else:
                    ledB.value = True
                    ledD.value = True
                    ledE.value = True
                    ledF.value = True
                    ledB.value = False
                    ledD.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 51:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledB.value = True
                    ledC.value = True
                    ledD.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledC.value = False
                    ledD.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledB.value = True
                    ledD.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledB.value = False
                    ledD.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 52:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledE.value = True
                    ledF.value = True
                    ledE.value = False
                    ledF.value = False
                else:
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledF.value = True
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
                    ledF.value = False
            elif event.key_number == 53:
                ledKDn.value = True
                KC=KC+1
                if octave.value:
                    ledA.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledE.value = False
                    ledF.value = False
                else:
                    ledA.value = True
                    ledC.value = True
                    ledD.value = True
                    ledE.value = True
                    ledF.value = True
                    ledA.value = False
                    ledC.value = False
                    ledD.value = False
                    ledE.value = False
                    ledF.value = False


        elif event.released:
            if event.key_number == 5:
                KeyCount()
            elif event.key_number == 6:
                KeyCount()
            elif event.key_number == 7:
                KeyCount()
            elif event.key_number == 8:
                KeyCount()
            elif event.key_number == 9:
                KeyCount()
            elif event.key_number == 10:
                KeyCount()
            elif event.key_number == 11:
                KeyCount()               
            elif event.key_number == 12:
                KeyCount()                
            elif event.key_number == 13:
                KeyCount()                
            elif event.key_number == 14:
                KeyCount()
            elif event.key_number == 15:
                KeyCount()
            elif event.key_number == 16:
                KeyCount()
            elif event.key_number == 17:
                KeyCount()
            elif event.key_number == 18:
                KeyCount()
            elif event.key_number == 19:
                KeyCount()
            elif event.key_number == 20:
                KeyCount()                  
            elif event.key_number == 21:
                KeyCount()               
            elif event.key_number == 22:
                KeyCount()                
            elif event.key_number == 23:
                KeyCount()
            elif event.key_number == 24:
                KeyCount()
            elif event.key_number == 25:
                KeyCount()
            elif event.key_number == 26:
                KeyCount()
            elif event.key_number == 27:
                KeyCount()
            elif event.key_number == 28:
                KeyCount()           
            elif event.key_number == 29:
                KeyCount()
            elif event.key_number == 30:
                KeyCount()
            elif event.key_number == 31:
                KeyCount()
            elif event.key_number == 32:
                KeyCount()
            elif event.key_number == 33:
                KeyCount()
            elif event.key_number == 34:
                KeyCount()                
            elif event.key_number == 35:
                KeyCount()            
            elif event.key_number == 36:
                KeyCount()           
            elif event.key_number == 37:
                KeyCount()           
            elif event.key_number == 38:
                KeyCount()
            elif event.key_number == 39:
                KeyCount()
            elif event.key_number == 40:
                KeyCount()
            elif event.key_number == 41:
                KeyCount()
            elif event.key_number == 42:
                KeyCount()
            elif event.key_number == 43:
                KeyCount()
            elif event.key_number == 44:
                KeyCount()
            elif event.key_number == 45:
                KeyCount()
            elif event.key_number == 46:
                KeyCount()
            elif event.key_number == 47:
                KeyCount()
            elif event.key_number == 48:
                KeyCount()
            elif event.key_number == 49:
                KeyCount()
            elif event.key_number == 50:
                KeyCount()
            elif event.key_number == 51:
                KeyCount()
            elif event.key_number == 52:
                KeyCount()
            elif event.key_number == 53:
                KeyCount()
